<template>
  <div class="main">
    <div class="title">
      <a class="active" href="/login">登录</a>

      <span>·</span>

      <a href="/register">注册</a>
    </div>

    <div class="sign-up-container">
      <el-form ref="userForm" :model="user">
        <el-form-item
          class="input-prepend restyle"
          prop="mobile"
          :rules="[
            { required: true, message: '请输入手机号码', trigger: 'blur' },
            { validator: checkPhone, trigger: 'blur' },
          ]"
        >
          <div>
            <el-input type="text" placeholder="手机号" v-model="user.mobile" />

            <i class="iconfont icon-phone" />
          </div>
        </el-form-item>

        <el-form-item
          class="input-prepend"
          prop="password"
          :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]"
        >
          <div>
            <el-input
              type="password"
              placeholder="密码"
              v-model="user.password"
            />

            <i class="iconfont icon-password" />
          </div>
        </el-form-item>

        <el-form-item prop="code">
          <div class="ver-code">
            <ul>
              <li class="input-code">
                <input
                  type="text"
                  v-model="user.code"
                  placeholder="请输入验证码"
                />
              </li>
              <li class="code-img">
                <img :src="verifCode" alt="" />
              </li>
            </ul>
            <a class="switch-btn" @click="changeCode()">看不清？换一张</a>
          </div>
        </el-form-item>

        <div class="btn">
          <input
            type="button"
            class="sign-in-button"
            value="登录"
            @click="submitLogin()"
          />
        </div>
      </el-form>

      <!-- 更多登录方式 -->

      <div class="more-sign">
        <h6>社交帐号登录</h6>

        <ul>
          <li>
            <a
              id="weixin"
              class="weixin"
              target="_self"
              :href="url + '/educenter/api/ucenter/wx/login'"
              ><i class="iconfont icon-weixin"
            /></a>
          </li>

          <li>
            <a id="qq" class="qq" target="_blank" href="#"
              ><i class="iconfont icon-qq"
            /></a>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>


<script>
import "~/assets/css/sign.css";

import "~/assets/css/iconfont.css";

import cookie from "js-cookie";

import loginApi from "@/api/sign/login";

export default {
  layout: "sign",

  data() {
    return {
      user: {
        mobile: "",
        password: "",
        code: "",
      },
      //显示信息
      loginInfo: {},
      url: "http://localhost:8222",
      verifCode: "",
    };
  },
  mounted() {
    this.changeCode();
  },
  methods: {
    //获取验证码
    changeCode() {
      this.verifCode = this.url + "/educenter/code?t=" + new Date().getTime();
    },
    //登录方法
    submitLogin() {
      //第一步：调用登录接口获取token信息
      loginApi.login(this.user).then((res) => {
        if (res.data.success) {
          //第二步：登录成功后获取token 放入cookie中
          //第一个参数：cookie名称
          //第二个参数：值
          //第三个参数:作用范围
          cookie.set("guli_token", res.data.data.token, {
            domain: "localhost",
          });

          //第四步 调用接口 根据token获取用户信息，为了首页展示
          loginApi.getLoginUserInfo().then((res) => {
            this.loginInfo = res.data.data.member;
            //获取返回用户信息，放到cookie里面
            cookie.set("guli_ucenter", this.loginInfo, {
              domain: "localhost",
            });
            //跳转页面
            window.location.href = "/";
          });
        }
      });
    },
    checkPhone(rule, value, callback) {
      // if (!/^1[34578]\d{9}$/.test(value)) {
      //   return callback(new Error("手机号码格式不正确"));
      // }
      // return callback();

      var emailCheck = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
      if (!emailCheck.test(value)) {
        return callback(new Error("邮箱号格式不正确"));
      }
      return callback();
    },
  },
};
</script>

<style>
.el-form-item__error {
  z-index: 9999999;
}
.ver-code {
  width: 100%;
  height: 50px;
  /* outline: 1px solid salmon; */
}
.ver-code ul {
  height: 100%;
  list-style: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.input-code input {
  width: 170px;
  height: 50px;
  outline: none;
  padding: 0 5px;
  border: 1px solid #bbb;
  border-radius: 3px;
}
.code-img {
  width: 100px;
  height: 50px;
  outline: 1px solid #aaa;
}
.code-img img {
  width: 100%;
}
.switch-btn {
  background: none;
  border: none;
  outline: none;
  color: rgb(35, 149, 255);
  font-size: 12px;
  cursor: pointer;
}
</style>